package homework;

public class HomeWork21 {
    public ListNode detectCycle(ListNode head) {
        ListNode slow=head;
        ListNode fast=head;
        if(head==null){
            return null;
        }
        while (fast!=null){
            if(fast.next!=null){
                fast=fast.next.next;
            }
            slow=slow.next;
            if(slow==fast){
                ListNode cur=head;
                while (slow!=cur){
                    slow=slow.next;
                    cur=cur.next;
                }
                return slow;
            }
        }
        return null;
    }
}
